<template>
  <el-container class="customer-home">
    <!-- ====== 顶部通栏（与店员端同风格） ====== -->
    <el-header class="header">
      <div class="header-left">
        <span class="store-name">{{ storeName }}</span>
      </div>
      <div class="header-right">
        <el-dropdown trigger="hover" placement="bottom-end">
          <!-- 圆形头像 -->
          <div class="avatar-icon">
            <el-icon size="22" color="#fff"><UserFilled /></el-icon>
          </div>

          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item disabled>
                <div style="text-align:center;padding:4px 0">
                  <div style="font-weight:bold">{{ userInfo.name }}</div>
                  <el-tag size="small" style="margin-top:4px">
                    {{ userInfo.role }}
                  </el-tag>
                </div>
              </el-dropdown-item>

              <!-- 统一入口：个人信息 -->
              <el-dropdown-item divided @click="$router.push('/customer/profile')">
                <el-icon><User /></el-icon>
                个人信息
              </el-dropdown-item>

              <el-dropdown-item divided @click="logout">
                <el-icon><SwitchButton /></el-icon>
                退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <!-- ====== 下方内容区 ====== -->
    <el-container class="main-wrapper">
      <!-- 左侧菜单 -->
      <el-aside width="200px" class="aside">
        <el-menu
          :default-active="$route.path"
          router
          class="aside-menu"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/customer/store-detail">
            <el-icon><House /></el-icon>
            <span>商家详情</span>
          </el-menu-item>
          <el-menu-item index="/customer/order">
            <el-icon><DishDot /></el-icon>
            <span>点餐</span>
          </el-menu-item>
          <el-menu-item index="/customer/my-order">
            <el-icon><Document /></el-icon>
            <span>我的订单</span>
          </el-menu-item>
          <!-- 个人信息已移到头像下拉栏 -->
        </el-menu>
      </el-aside>

      <!-- 右侧内容 -->
      <el-main class="content">
        <router-view />
      </el-main>
    </el-container>

    <!-- 弹窗：改密码（与店员端共用） -->
    <PasswordDialog ref="pwdDlg" />
  </el-container>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { jwtDecode } from 'jwt-decode'
import { useAuthStore } from '@/stores/auth'
import PasswordDialog from '@/components/PasswordDialog.vue'
import {
  House, DishDot, Document, UserFilled, User, SwitchButton
} from '@element-plus/icons-vue'

interface JwtPayload {
  sub: string
  username: string
  role: 1 | 2 | 3 | 4
  storeName: string
  avatar?: string
}

const auth = useAuthStore()
const router = useRouter()

/* 1. 解析 token */
const payload = computed<JwtPayload>(() =>
  auth.token ? jwtDecode<JwtPayload>(auth.token) : ({} as JwtPayload)
)

/* 2. 顶部信息 */
const storeName = computed(() => payload.value.storeName || '未知门店')
const userInfo = computed(() => ({
  name: payload.value.username || '匿名用户',
  role: ['顾客', '服务员', '店长', '管理员'][payload.value.role - 1]
}))

/* 3. 退出 */
function logout() {
  auth.clear()
  router.replace('/login')
}

/* 4. 弹窗实例 */
const pwdDlg = ref()
</script>

<style scoped>
.customer-home {
  height: 100vh;
}

/* ====== 顶部栏（与店员端同色） ====== */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #408bfc;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  padding: 0 16px;
  font-size: 14px;
  color: #fff;
}
.store-name {
  font-weight: bold;
  font-size: 16px;
}
.avatar-icon {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #409eff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* ====== 侧边栏 ====== */
.aside {
  background: #304156;
}
.aside-menu {
  border-right: none;
}

/* ====== 内容区 ====== */
.content {
  padding: 16px;
  background: #f5f5f5;
}

/* ====== 暗黑模式（自动跟随系统） ====== */
@media (prefers-color-scheme: dark) {
  .header {
    background: #2b2c2e;
  }
  .aside {
    background: #1d1e1f;
  }
  .content {
    background: #181818;
  }
}
</style>